package com.zhousb.deviceManager.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.common.PageRequest;
import com.zhousb.deviceManager.common.R;
import com.zhousb.deviceManager.common.ResponseResult;
import com.zhousb.deviceManager.exception.CustomerException;
import com.zhousb.deviceManager.model.bo.ProductBo;
import com.zhousb.deviceManager.model.so.ProductLogSo;
import com.zhousb.deviceManager.model.so.ProductSo;
import com.zhousb.deviceManager.model.so.SimpleSo;
import com.zhousb.deviceManager.service.ProductService;
import com.zhousb.deviceManager.ws.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/product")
@Tag(name = "产品清单")
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;
    @PostMapping("/page")
    @Operation(summary = "产品列表")
    public ResponseResult page(@RequestBody PageRequest<ProductSo> so) {
        return R.success(productService.page(new Page<>(so.getCurrent(), so.getSize()), so.getQuery()));
    }

    @PostMapping("/price/page")
    @Operation(summary = "产品报价列表")
    public ResponseResult pricePage(@RequestBody PageRequest<ProductSo> so) {
        return R.success(productService.pricePage(new Page<>(so.getCurrent(), so.getSize()), so.getQuery()));
    }

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    public ResponseResult create(@RequestBody ProductBo bo) {
        log.info("创建产品：{}",JSONUtil.toJsonStr(bo));
        return R.success(productService.create(bo));
    }

    @PostMapping("/update")
    @Operation(summary = "更新产品")
    public ResponseResult update(@RequestBody ProductBo bo) {
        log.info("更新产品信息:{}",JSONUtil.toJsonStr(bo));
        return R.success(productService.update(bo));
    }

    @GetMapping("/delete")
    @Operation(summary = "删除产品")
    public ResponseResult delete(@RequestParam("productId")String productId) {
        log.info("删除产品：{}",productId);
        return R.success(productService.delete(productId));
    }

    @PostMapping("/log")
    @Operation(summary = "库存日志")
    public ResponseResult log(@RequestBody PageRequest<ProductLogSo> so) {
        return R.success(productService.log(new Page<>(so.getCurrent(), so.getSize()), so.getQuery()));
    }

    @PostMapping("/export")
    @Operation(summary = "库存导出")
    public void export(@RequestBody ProductSo so, HttpServletResponse response) {
        try {
            productService.export(so, response);
        }catch (IOException e){
            e.printStackTrace();
            response.reset();
            throw new CustomerException("导出异常");
        }
    }

    @GetMapping("/watch")
    @Operation(summary = "监听产品")
    public ResponseResult watch(@RequestParam("productId")String productId) {
        return R.success(productService.watch(productId));
    }

    @GetMapping("/unwatch")
    @Operation(summary = "监听产品")
    public ResponseResult unwatch(@RequestParam("productId")String productId) {
        return R.success(productService.unwatch(productId));
    }

    @GetMapping("/current")
    @Operation(summary = "获取产品最新数据")
    public ResponseResult current(@RequestParam("productId")String productId) {
        return R.success(productService.current(productId));
    }
}
